let wxparse = require("../../wxParse/wxParse.js")
let http = require('../../utils/api.js');
import {
    toData,
    myHttps
} from '../../utils/util.js';
import {
    myHttpss
} from '../../utils/json.js';
Page({
    data: {
        hiddenLoading: false,
        comments: [], //评论接口获取得所有数据
        total: '', //评论得数量
        fid: '', //某条评论的id
        val: '', //评论输入框的内容
        contents: '', //走评论接口返回给我的数据
        huifushui: '1',
        xianshi: false, //控制发送按钮的颜色
        huii: false, //自动聚焦
        zhankai: false,
        writecomments: false, //输入内容的输入框
        themeId: '', //关注主题参数
        scrollTop: 0,
        floorstatus: false,
        articleId: '', //文章id
        massage: '',
        isFocus: '', //是否关注
        collect: false, //未收藏
        comment: false, //显示评论
        content: '', //写评论输入的内容
        showLike: true,
        goods: '', //评论点赞
        dkheight: '', //插入html的高度
        contentheight: '', //正文的高度
        uid: '',
        fromid: '',
        cid: '',
        data: '', //详情页的所有数据
        dkcontent: '', //内容区的数据  
        userId: '', //用户id
        userInfo: '', //用户信息
        nologin: '',
        isFav: '', //是否收藏
        nologin: '',
        first: '',
        likecount: '', //文章的点赞量
        reply: '', //回复接口请求到的数据
        islike: '', //是否点赞
        themeList: '', //文章主题列表第一条
        essaysid: '', //文章id
        page: 1, //当前页面 
        size: 10, //页容量
        toView: 'inToViewcontent',
        index: '', //评论的index
        img: '', //分享页面得图片
        imgs: '',
        contentss: '',
        titless: '',
        idss: '',
        indexcontent: '', //从首页带过来得内容部分 用于分享给朋友
        role: '', //判断是不是作者身份
        codes: false, //二维码弹框
        reviewList: [], //解读列表
        jieTotal: '', //解读数量
    },

    onLoad: function(e) {
        if (e.content) {
            this.setData({
                indexcontent: e.content,
            })
        }
        this.setData({
            articleId: e.id,
            nologin: wx.getStorageSync('no_login'),
            first: wx.getStorageSync('first'),
            userId: wx.getStorageSync('userId'),
        })
        this.reviewList(this.data.articleId,this.data.size, this.data.page) //解读列表

    },
    //下拉刷新从新初始化页码,条数
    onPullDownRefresh: function () {
        var date = new Date();
        //下拉三个点消失
        wx.stopPullDownRefresh();
        this.setData({
            reviewList:[],
            size: 10,
            page: 1,
            // refreshTime: date.toLocaleTimeString(),
            // hideHeader: false
        })
        this.reviewList(this.data.articleId,this.data.size, this.data.page);
    },
    // 上拉加载数据
    onReachBottom: function () {
        this.setData({
            size: 10,
            page: this.data.page + 1,
        })
        this.reviewList(this.data.articleId,this.data.size, this.data.page);
    },
    // 跳转解读详情
    reviewsDetails(e) {
        // console.log(e.currentTarget.dataset.id)
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../unscrambleDetails/unscrambleDetails?id=' + id,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    //解读点赞接口
    showLikes: function(e) {
        this.setData({
            hiddenLoading: false
        })
        let index = e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id //解读id
        let url = http.api.reviewGood + id + '/like';
        myHttps({
            url: url,
            method: "POST",
        }).then(res => {
            // console.log(res)
            if (res.code == 1) {
                this.data.reviewList[index].like = true
                this.data.reviewList[index].like_count = this.data.reviewList[index].like_count + 1
                this.setData({
                    reviewList: this.data.reviewList,
                    hiddenLoading: true
                })
            }
        }).catch(err => {})
    },
    // 解读取消点赞  接口
    showLikenos: function(e) {
        this.setData({
            hiddenLoading: false
        })
        let index = e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id //解读id
        let url = http.api.reviewGood + id + '/like';
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            // console.log(res)
            if (res.code == 1) {
                this.data.reviewList[index].like = false
                this.data.reviewList[index].like_count = this.data.reviewList[index].like_count - 1
                this.setData({
                    reviewList: this.data.reviewList,
                    hiddenLoading: true
                })
            }
        }).catch(err => {})
    },
    // 点击写解读 出弹框
    writereview() {
        this.setData({
            huifushui: '5',
            huii: true,
            writecomments: true,
        })
    },
    // 写解读接口
    writereviews(e) {
        let id = this.data.articleId;
        let url = http.api.writecomment + id + '/reviews';
        let obj = {
            content: this.data.val
        }
        myHttpss({
            url: url,
            method: "POST",
            data: obj
        }).then(res => {
            if (res.code == 1) {
                // this.setData({
                //     hiddenLoading: true
                // })
                this.reviewList()
            }
        }).catch(err => {})
    },
    // 获取解读列表
    reviewList(id,size,page) {
        let url = http.api.writecomment + id + '/reviews?page=' + page + '&size=' + size;
        myHttpss({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                for (var i = 0; i < res.data.content.length; i++) {
                    res.data.content[i].update_time = toData(res.data.content[i].update_time); //时间格式化
                    this.data.reviewList.push(res.data.content[i])
                }
                this.setData({
                    reviewList: this.data.reviewList,
                    hiddenLoading: true
                })
            }
        }).catch(err => {})
    },
    // 关闭弹框二维码
    closess() {
        this.setData({
            codes: false, //弹框 二维码
        })
    },
    //输入字变色
    shuru(e) {
        let val = e.detail.value;
        this.setData({
            val: val
        })
        // console.log(this.data.val)
        if (e.detail.value == '') {
            this.setData({
                xianshi: false
            });
        } else {
            this.setData({
                xianshi: true
            });
        }

    },
    //关闭输入弹框
    closebg() {
        this.setData({
            writecomments: false
        })
    },
    //发送接口
    send() {
        // console.log(this.data.huifushui)
        this.setData({
            hiddenLoading: false
        })
        var that = this
        if (this.data.huifushui == 2) {
            //写评论接口
            this.getWritecomment()
        } else if (this.data.huifushui == 4) {
            //回复评论接口
            this.replyComments()
        } else if (this.data.huifushui == 5) {
            //写解读接口
            this.writereviews()
        }
    },
    // 写评论  接口
    writeComment() {
        this.setData({
            huifushui: '2',
            huii: true,
            writecomments: true,
        })
    },
    // 写评论 点击完成 走接口
    getWritecomment() {
        let id = this.data.articleId;
        let that = this
        let url = http.api.writecomment + id + '/comments';
        let obj = {
            content: this.data.val,
        }
        myHttpss({
            url: url,
            method: "POST",
            data: obj
        }).then(res => {
            if (res.code == 1) {
                this.data.comments.splice(0, 9);
                // console.log(this.data.comments)
                this.setData({
                    comments: this.data.comments,
                    toView: 'inToView' + 'pinglun',
                    comment: true,
                    hiddenLoading: true
                })
                this.articleComments(1, 10, 'u')
            }
        }).catch(err => {})
    },
    // 点击评论 出现弹窗
    replyComment(e) {
        let id = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index
        this.setData({
            index: index,
            id: id,
            huifushui: '4',
            huii: true,
            writecomments: true,
            fromid: e.currentTarget.dataset.id,
            uid: e.currentTarget.dataset.uid,
        })
    },
    // 点击回复评论 接口
    replyComments() {
        var that = this
        let id = this.data.articleId;
        let url = http.api.writecomment + id + '/comments';
        let index = this.data.index
        let obj = {
            to_uid: this.data.uid,
            content: this.data.val,
            f_id: this.data.fromid
        }
        myHttpss({
            url: url,
            method: "POST",
            data: obj
        }).then(res => {
            if (res.code == 1) {
                this.data.comments.splice(0, 9);
                // console.log(this.data.comments);
                this.setData({
                    comments: this.data.comments,
                    toView: 'inToView' + 'pinglun',
                    comment: true,
                    hiddenLoading: true
                })
                this.articleComments(1, 10, 'u')
            }
        }).catch(err => {})
        this.setData({
            zhankai: true
        })
    },
    onShareAppMessage: function(options) {;
        var that = this;
        // 设置菜单中的转发按钮触发转发事件时的转发内容　
        var shareObj = {
            title: '头号财讯',
            path: 'pages/indexs/index',
            imageUrl: '../../images/logo.png', //不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
            success: function(res) { // 转发成功之后的回调　　　　
                if (res.errMsg == 'shareAppMessage:ok') {}
            },
            fail: function() {},
        }
        // 来自页面内的按钮的转发　
        if (options.from == 'button') {
            var eData = options.target.dataset;
            console.log(options)
            return {
                title: eData.title,
                imageUrl: eData.img,
                path: '/pages/unscrambleDetails/unscrambleDetails?id=' + eData.id
            }
        }
        return shareObj; // 返回shareObj


    },
})